package com.matt.controller.pad;

import com.matt.domain.LeaveRecord;
import com.matt.foundation.constant.RequestConstant;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.service.LeaveRecordService;
import com.matt.service.remoteCall.SsoCallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/pad/leaveRecord")
@Slf4j
public class PadLeaveRecordController {


    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private LeaveRecordService leaveRecordService ;


    /**
     * 微信端新增家长请假
     * @param parentPhone
     * @param childrenId
     * @param leaveDate
     * @param leaveType
     * @param content
     * @return
     */
    @RequestMapping("/addParentsRecord")
    public StatusResponse addParentsRecord(String parentPhone,String childrenId,String leaveDate,Integer leaveType, String content){
        LeaveRecord record = leaveRecordService.addParentsRecord(parentPhone, childrenId, leaveDate, leaveType, content);
        return StatusResponse.success(record);
    }

    /**
     * app新增老师请假
     * @param ticket
     * @param leaveDate
     * @param leaveType
     * @param content
     * @return
     */
    @RequestMapping("/addTeacherRecord")
    public StatusResponse addTeacherRecord(@RequestHeader(RequestConstant.TICKET) String ticket,String leaveDate,Integer leaveType, String content){
        StaffDTO staff = cache.getStaff(ticket);

        String teacherName = staff.getRealName();
        String areaId = staff.getAreaId();

        LeaveRecord record = leaveRecordService.addTeacherRecord(areaId, staff.getId(), teacherName, leaveDate, leaveType, content);
        return StatusResponse.success(record);
    }


    /**
     * 微信历史请假列表
     */
    @RequestMapping("/getWxLeaveRecord")
    public StatusResponse getWxLeaveRecord(String childrenId, String month, PagerQuery query){

        Page<LeaveRecord> recordPage = leaveRecordService.getWxLeaveRecord(childrenId, month, query.getPageNum(), query.getPageSize());


        return StatusResponse.success(recordPage);
    }

    /**APP
     * 查询请假列表
     */
    @RequestMapping("/getAppLeaveRecord")
    public StatusResponse getAppLeaveRecord(@RequestHeader(RequestConstant.TICKET) String ticket,String source, PagerQuery query){
        String areaId = cache.getAreaId(ticket);
        String staffId = cache.getStaffId(ticket);
        Page<LeaveRecord> recordPage = leaveRecordService.getAppLeaveRecord(areaId, staffId, source, query.getPageNum(), query.getPageSize());


        return StatusResponse.success(recordPage);
    }


    /**
     * 批准请假
     * @return
     */
    @RequestMapping("/checkLeaveRecord")
    public StatusResponse checkLeaveRecord(@RequestHeader(RequestConstant.TICKET) String ticket, String leaveRecordId) {

        String staffId = cache.getStaffId(ticket);
        StaffDTO staff = ssoCallService.getStaffDTO(staffId);
        String staffName = staff.getRealName();

        leaveRecordService.checkLeaveRecord(staffId, staffName, leaveRecordId, null);
        return StatusResponse.success();
    }

    /**
     * 批准请假
     * @return
     */
    @RequestMapping("/refuseLeaveRecord")
    public StatusResponse refuseLeaveRecord(@RequestHeader(RequestConstant.TICKET) String ticket, String leaveRecordId, String refuseReason) {

        String staffId = cache.getStaffId(ticket);
        StaffDTO staff = ssoCallService.getStaffDTO(staffId);
        String staffName = staff.getRealName();

        leaveRecordService.refuseLeaveRecord(staffId, staffName, leaveRecordId, refuseReason);
        return StatusResponse.success();
    }

    /**
     * 删除请假
     * @return
     */
    @RequestMapping("/deleteLeaveRecord")
    public StatusResponse deleteLeaveRecord(@RequestHeader(RequestConstant.TICKET) String ticket, String leaveRecordId) {

        String staffId = cache.getStaffId(ticket);
        StaffDTO staff = ssoCallService.getStaffDTO(staffId);
        String staffName = staff.getRealName();

        leaveRecordService.deleteLeaveRecord(staffId, staffName, leaveRecordId);
        return StatusResponse.success();
    }

    /**APP
     * 根据日期查询请假
     */
    @RequestMapping("/getAppLeaveRecordByDate")
    public StatusResponse getAppLeaveRecordByDate(@RequestHeader(RequestConstant.TICKET) String ticket,String beginDate,String endDate){

        String staffId = cache.getStaffId(ticket);
        List<LeaveRecord> list = leaveRecordService.getMyLeaveRecordByDate(staffId, beginDate,endDate);

        return StatusResponse.success(list);
    }
}
